package com.have.blog.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.have.blog.admin.constant.AdminMessageConstant;
import com.have.blog.admin.domain.dto.WebNavigationBarDto;
import com.have.blog.admin.service.AdminWebNavbarService;
import com.have.blog.base.constant.BaseConstant;
import com.have.blog.base.enums.BlogStatus;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.model.mapper.WebNavigationBarMapper;
import com.have.blog.model.models.WebNavigationBar;
import com.have.blog.model.models.WebNavigationBarExample;
import com.have.blog.utils.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@SuppressWarnings("all")
public class AdminWebNavbarServiceImpl implements AdminWebNavbarService {

    @Autowired
    private WebNavigationBarMapper webNavigationBarMapper;

    @Override
    public List<WebNavigationBarDto> getList(WebNavigationBarDto webNavbarDto) {
        PageHelper.startPage(webNavbarDto.getPageNum(), webNavbarDto.getPageSize());
        WebNavigationBarExample example = new WebNavigationBarExample();
        WebNavigationBarExample.Criteria criteria = example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        if (!StringUtils.isEmpty(webNavbarDto.getKeyword()) && !StringUtils.isEmpty(webNavbarDto.getKeyword().trim())) {
            criteria.andNameLike("%" + webNavbarDto.getKeyword() + "%");
        }
        if (webNavbarDto.getNavbarLevel() != 0) {
            criteria.andNavbarLevelEqualTo(webNavbarDto.getNavbarLevel());
        }
        example.setOrderByClause("sord desc");
        List<WebNavigationBar> webNavbarList = webNavigationBarMapper.selectByExample(example);
        List<WebNavigationBarDto> resultList = MyBeanUtils.copyListProperties(webNavbarList, WebNavigationBarDto::new);
        return resultList;
    }

    @Override
    public List<WebNavigationBarDto> getAllList() {
        WebNavigationBarExample example = new WebNavigationBarExample();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andNavbarLevelEqualTo(BaseConstant.NUM_ONE);
        example.setOrderByClause("sort desc");
        List<WebNavigationBar> webNavbarList = webNavigationBarMapper.selectByExample(example);
        List<WebNavigationBarDto> resultList = MyBeanUtils.copyListProperties(webNavbarList, WebNavigationBarDto::new);

        // 查询子目录
        List<Integer> uids = new ArrayList<>();
        resultList.forEach(item -> {
            uids.add(item.getUid());
        });
        example.clear();
        example.createCriteria()
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus())
                .andParentUidIn(uids);
        example.setOrderByClause("sort desc");
        List<WebNavigationBar> childList = webNavigationBarMapper.selectByExample(example);
        List<WebNavigationBarDto> webNavigationBarDtos = MyBeanUtils.copyListProperties(childList, WebNavigationBarDto::new);
        Map<Integer, List<WebNavigationBarDto>> map = new HashMap<>();
        for (WebNavigationBarDto item : webNavigationBarDtos) {
            List<WebNavigationBarDto> tempList = map.get(item.getParentUid());
            if (tempList == null) {
                tempList = new ArrayList<>();
                map.put(item.getParentUid(), tempList);
            }
            tempList.add(item);
        }
        return resultList;
    }

    @Override
    public Wrapper addWebNavigationBar(WebNavigationBarDto webNavbarDto) {
        if (webNavbarDto.getNavbarLevel() == BaseConstant.NUM_ONE) {
            webNavbarDto.setParentUid(null);
        }
        WebNavigationBar WebNavigationBar = new WebNavigationBar();
        MyBeanUtils.copyProperties(webNavbarDto, WebNavigationBar);
        WebNavigationBar.setStatus(BlogStatus.ENABLE.getStatus());
        int count = webNavigationBarMapper.insert(WebNavigationBar);
        if (count > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.INSERT_SUCCESS);
        } else {
            return Wrapper.failed(AdminMessageConstant.INSERT_FAIL);
        }
    }

    @Override
    public Wrapper editWebNavigationBar(WebNavigationBarDto webNavbarDto) {
        if (webNavbarDto.getNavbarLevel() == BaseConstant.NUM_ONE) {
            webNavbarDto.setParentUid(null);
        }
        WebNavigationBar webNavbar = webNavigationBarMapper.selectByPrimaryKey(webNavbarDto.getUid());
        webNavbar.setStatus(webNavbarDto.getStatus());
        webNavbar.setIcon(webNavbarDto.getIcon());
        webNavbar.setIsJumpExternalUrl(webNavbarDto.getIsJumpExternalUrl());
        webNavbar.setName(webNavbarDto.getName());
        webNavbar.setSummary(webNavbar.getSummary());
        webNavbar.setParentUid(webNavbarDto.getParentUid());
        webNavbar.setSort(webNavbarDto.getSort());
        webNavbar.setIsShow(webNavbarDto.getIsShow());
        webNavbar.setUrl(webNavbarDto.getUrl());
        webNavbar.setNavbarLevel(webNavbarDto.getNavbarLevel());
        int count = webNavigationBarMapper.updateByPrimaryKeySelective(webNavbar);
        if (count > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.UPDATE_SUCCESS);
        } else {
            return Wrapper.failed(AdminMessageConstant.UPDATE_FAIL);
        }
    }

    @Override
    public Wrapper deleteWebNavigationBar(WebNavigationBarDto webNavbarDto) {
        WebNavigationBarExample example = new WebNavigationBarExample();
        example.createCriteria()
                .andParentUidEqualTo(webNavbarDto.getUid())
                .andStatusEqualTo(BlogStatus.ENABLE.getStatus());
        long num = webNavigationBarMapper.countByExample(example);
        if (num > 0) {
            return Wrapper.failed(AdminMessageConstant.CHILDREN_MENU_UNDER_THIS_MENU);
        }
        WebNavigationBar webNavbar = webNavigationBarMapper.selectByPrimaryKey(webNavbarDto.getUid());
        webNavbar.setStatus(BlogStatus.DISABLE.getStatus());
        int count = webNavigationBarMapper.updateByPrimaryKeySelective(webNavbar);
        if (count > 0) {
            return Wrapper.successWithMessage(AdminMessageConstant.DELETE_SUCCESS);
        }
        return Wrapper.failed(AdminMessageConstant.DELETE_FAIL);
    }
}
